﻿Imports System.Windows.Forms

Public Class frmAddOrderHeaders
    Inherits dbiWSF.Dialogs.dlgDefault
    Dim l_shipment As dbiWSF.FrameworkObjects.objShipment   'Stores the shipment being configured.
    Dim l_loadingdock As dbiWSF.FrameworkObjects.objLoadingDock 'Stores the loading dock on which the shipment is scheduled.

    Public Sub New(ByVal Shipment As dbiWSF.FrameworkObjects.objShipment, ByVal LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        'Sets the form's variables to the values passed in when the form is created.
        l_loadingdock = LoadingDock
        l_shipment = Shipment

        'Create the columns for the list.
        Dim Column1 As New Dbi.WinControl.dbiColumnItem("Order", 200)
        Dim Column2 As New Dbi.WinControl.dbiColumnItem("CargoType(s)", 300)
        Dim Column3 As New Dbi.WinControl.dbiColumnItem("On Shipment")

        Column3.DataType = Dbi.WinControl.enumDataType.Boolean
        Column3.Format.CheckBox = Dbi.enumCheckBox.Standard

        'Add the columns to the dbiList's Columns collection.
        Me.DbiList1.Columns.Add(Column1)
        Me.DbiList1.Columns.Add(Column2)
        Me.DbiList1.Columns.Add(Column3)

        'Set the title for the form.
        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Outbound Then
            Me.DbiLabel1.Text = "Unassigned Purchase Orders that contain one or more of the Cargo Types " + vbCrLf + "accepted by the Loading Dock the Shipment is scheduled in ..."
        Else
            Me.DbiLabel1.Text = "Unassigned Invoices that contain one or more of the Cargo Types " + vbCrLf + "accepted by the Loading Dock the Shipment is scheduled in ..."
        End If

    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        'Iterate through the items in the list
        For Each curNode As Dbi.WinControl.dbiNodeItem In Me.DbiList1.Items
            If curNode.GetCellText(2) = "1" Then
                'If the user checked the checkbox in the last column add the order to the shipment.
                'NOTE: Set the ShipmentID on the OrderHeader to create the link between the two at the data layer.
                curNode.Tag.ShipmentID = l_shipment.ShipmentID

                'Add the OrderHeader to the collection stored on the shipment object.
                l_shipment.OrderHeaders.Add(curNode.Tag)

            End If

        Next

        Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DbiList1_FirstDraw(ByVal sender As Object, ByVal e As System.EventArgs) Handles DbiList1.FirstDraw
        'NOTE: This event fires when the dbiList control is first drawn in the form.
        Dim OrderType As String

        'Determine which type of orders are applicable to the shipment based on the shipment type.
        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Inbound Then
            OrderType = "Invoice"
        Else
            OrderType = "Purchase Order"
        End If

        Me.DbiList1.TitleText = "Add Orders to Shipment: " + l_shipment.Description

        'Create a collection of order headers.
        Dim myOrders As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objOrderHeader)

        'Populate the collection with all of the unassigned order headers (those orders that have not yet been assigned to a shipment.
        myOrders = myWSFObjects.GetUnassignedOrderHeaders()

        'Loop through all of the OrderHeaders currently unassigned to any shipment and populate the list with their details.
        For Each Order As dbiWSF.FrameworkObjects.objOrderHeader In myOrders

            If Order.OrderEntryType = OrderType Then
                'Use a LINQ query to select a distinct count of cargo types on each order.
                'NOTE:  This is used to avoid displaying redundant information to the user.
                Dim OrderCargo = From OrderDetails As dbiWSF.FrameworkObjects.objOrderDetail In Order.OrderDetails _
                                 Where l_loadingdock.CargoTypes.Contains(OrderDetails.CargoTypeID) _
                                 Order By OrderDetails.CargoTypeID _
                                 Distinct Select OrderDetails.CargoTypeID

                If OrderCargo.Count > 0 Then

                    'Create a new node item for the list.
                    Dim newNode As New Dbi.WinControl.dbiNodeItem(Order.OrderEntryID + ";")

                    'Build the cargo type description.
                    For Each Cargo As System.Guid In OrderCargo.Distinct
                        newNode.Text += " | " + myWSFObjects.GetCargoDescripFromID(Cargo)
                    Next

                    'Place the order object in the tag property of the node item representing it.
                    newNode.Tag = Order

                    'Iterate through the order details related to the order header.
                    For Each OrderDetail As dbiWSF.FrameworkObjects.objOrderDetail In Order.OrderDetails

                        'Create a new node item to represent the order detail.
                        Dim DetailNode As New Dbi.WinControl.dbiNodeItem()
                        Dim x As New NorthwindProduct

                        'Determine from which table to select the information on the items in the order.
                        'NOTE:  This is required because the Sales (Invoice) and Purchase Order details are stored in separate tables in the northwinds database
                        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Outbound Then
                            x = GetProductByID(dtPODetails.Rows.Find(OrderDetail.LineNumber).Item("Product ID").ToString)
                        Else
                            x = GetProductByID(dtOrderDetails.Rows.Find(OrderDetail.LineNumber).Item("Product ID").ToString)
                        End If

                        'Set the column values for the node item.
                        DetailNode.Text = OrderDetail.LineNumber.ToString + ":  " + x.ProductName + " | " + myWSFObjects.GetCargoDescripFromID(OrderDetail.CargoTypeID)

                        'Store the detail object in the tag property of the node item representing it.
                        DetailNode.Tag = OrderDetail

                        'Make the new detail node a header item.
                        DetailNode.Header = True

                        'Add the detail node item to the order header node item.
                        newNode.Items.Add(DetailNode)

                    Next    'Order Detail.

                    'Add the order header node to the dbiList control.
                    Me.DbiList1.Items.Add(newNode)

                End If

            End If

        Next    'Order Header.

        'Apply the current style, selected in the main application, to the dbiList control
        Dim curStyle As New dbiWSF.ControlLibrary.dbiWSFStyleControls
        Dim curListOptions As New dbiWSF.ControlLibrary.dbiStylerOptions.dbiListStyleOptions

        curStyle.ImportXMLWithXMLString(curListOptions, Me.DbiList1, dbiWSF.ControlLibrary.dbiStylerOptions.GetResources(curStyleName).dbiListXML)

    End Sub

End Class
